Skip to content

use Win32 file APIs and widePath for file ops to support long filenames #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2025

Conversation

daveinglis
Copy link
Contributor

No description provided.

@daveinglis
Copy link
Contributor Author

@swift-ci test

@daveinglis daveinglis changed the title use to Win32 file APIs and widePath for file ops to support long filenames use Win32 file APIs and widePath for file ops to support long filenames Aug 7, 2025
Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this!

}
if (RemoveDirectoryW(wpath.data())) {
return 0;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary } else {. Just use } and the rest of the body can follow.

@daveinglis daveinglis force-pushed the windows_long_file_fix branch from 2a26b43 to e0c6ead Compare August 8, 2025 13:54
@daveinglis
Copy link
Contributor Author

@swift-ci test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor nits, but looks good to me.

llvm::SmallVector<wchar_t, MAX_PATH> wPath;
if (llvm::sys::path::widenPath(path, wPath))
return std::string();
CreateDirectoryW(wPath.data(), NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to log the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a lot of logging... Are you thinking just a fprintf maybe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ... at least we would have something to go on if it ever failed.

@daveinglis daveinglis force-pushed the windows_long_file_fix branch from e0c6ead to 3ff8d69 Compare August 8, 2025 17:25
@daveinglis
Copy link
Contributor Author

@swift-ci test

@daveinglis daveinglis force-pushed the windows_long_file_fix branch from 3ff8d69 to c9736f5 Compare August 11, 2025 12:41
@daveinglis
Copy link
Contributor Author

@swift-ci test

@daveinglis daveinglis force-pushed the windows_long_file_fix branch from c9736f5 to bb4eef8 Compare August 11, 2025 14:07
@daveinglis
Copy link
Contributor Author

@swift-ci test

@daveinglis
Copy link
Contributor Author

@swift-ci please smoke test

#else
return ::rmdir(path);
#endif
}

int sys::stat(const char *fileName, StatStruct *buf) {
#if defined(_WIN32)
return ::_stat(fileName, buf);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compnerd Would it be simpler to use _wstat, _wrmdir, etc., vs the Win32 functions? They support long paths too, as they are just wrappers around Win32 anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so there would need some char to wchar_t conversion here to use those, so Im not sure it saves that much since the widePath API does this already.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might've misunderstood what I meant. I was suggesting that instead of writing thirty lines of code, you simply call:

llvm::SmallVector<wchar_t, MAX_PATH> wfilename;
if (llvm::sys::path::widenPath(fileName, wfilename)) {
    errno = EINVAL;
    return -1;
}
return ::_wstat(wfilename, buf);

...as opposed to GetFileAttributesExW and all the conversion ceremony that follows. I'm also concerned this is subtly changing behavior as you're filling in the stat info differently from how _stat and _wstat do it, now.

I think you should:

  • Switch CreateDirectoryW to _wmkdir
  • Switch RemoveDirectoryW to _wrmdir
  • Switch GetFileAttributesExW to _wstat
  • Switch DeleteFileW to _wunlink

That will ensure error handling and behavior is consistent with previously.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these _w functions accept the //?/ prefixed paths?

Copy link
Member

@compnerd compnerd Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Microsoft implementation does support the NT style paths I believe but they are disjoint from Win32. e.g. usingSetEnvironmentVariableW and wgetenv would give unexpected results. I think that using win32 might unify some behaviors.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these _w functions accept the //?/ prefixed paths?

Like I said, they just wrap the underlying Win32 functions with a little ceremony on top. So by definition, yes. e.g. the implementation of _wmkdir literally just calls CreateDirectoryW and does nothing but that other than set errno to an appropriate value based on GetLastError.

ucrt's source is also available in the Windows SDK and you can inspect it.

e.g. usingSetEnvironmentVariableW and wgetenv would give unexpected results.

Environment is a little different than the other functions.

@daveinglis daveinglis merged commit fa78e46 into swiftlang:main Aug 14, 2025
6 checks passed
@aschwaighofer
Copy link
Contributor

It seems like the windows-toolchain build failed after this commit.

https://ci-external.swift.org/job/swift-main-windows-toolchain/1526

Looking at the commits that were part of the first failing run. This seemed like the most likely culprit given that it modifies a function called mkdir.

Pull request testing also seems blocked e.g https://ci-external.swift.org/view/Pull%20Request/job/swift-PR-windows/44356/

@bnbarham
Copy link
Contributor

bnbarham commented Aug 15, 2025

I mentioned this in the revert, but the issue here is that PR testing was testing tip rather than the PR itself. I'll edit this comment once that's fixed. EDIT: That should be fixed now. Worth checking the commit in the next PR run though 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants